package com.student.status.dao.daoimpl;

import com.student.status.dao.UserDao;
import com.student.status.model.UserInfoEntity;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;
    private int count = 0;
    private Session session;
    private Transaction transaction;

    private Session getCurrentSession() {
        return this.sessionFactory.openSession();
    }

    @Override
    public UserInfoEntity selectById(UserInfoEntity userInfoEntity) {
        return getCurrentSession().get(UserInfoEntity.class, userInfoEntity.getId());
    }

    @Override
    public UserInfoEntity selectName(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        String hql = "select u from UserInfoEntity u where u.userLoginName = :name";
        Query query = session.createQuery(hql, UserInfoEntity.class);
        query.setParameter("name",userInfoEntity.getUserLoginName());
        UserInfoEntity user = (UserInfoEntity)query.uniqueResult();
        session.close();
        return user;
    }

    @Override
    public UserInfoEntity selectPwd(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        String hql = "select u from UserInfoEntity u where u.userLoginName = :name and u.plaintPwd = :pwd";
        Query query = session.createQuery(hql, UserInfoEntity.class);
        query.setParameter("name",userInfoEntity.getUserLoginName());
        query.setParameter("pwd",userInfoEntity.getUserLoginPwd());
        UserInfoEntity user = (UserInfoEntity)query.uniqueResult();
        session.close();
        return user;
    }

    @Override
    public List<UserInfoEntity> selectAll() {
        session = getCurrentSession();
        String hql = "select u from UserInfoEntity u";
        Query<UserInfoEntity> query = session.createQuery(hql, UserInfoEntity.class);
        List<UserInfoEntity> list = query.getResultList();
        session.close();
        return list;
    }

    @Override
    public int saveUser(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        try{
            session.save(userInfoEntity);
            session.beginTransaction().commit();
            count = 1;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    //更新由于其他问题，先session.beginTransaction();在进行操作
    @Override
    public int updateUser(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        try{
            String hql = "update UserInfoEntity set userName = :uname,"
                    + "userHeadUrl = :uhead, userSex = :usex, userEmail = :uemail,"
                    + "userBrithday = :ubrit, userAddress = :uaddre, userPhone = :upho,"
                    + "userTel = :utel where id = :id";

            transaction = session.beginTransaction();
            Query query = session.createQuery(hql);
            query.setParameter("id",userInfoEntity.getId());
            query.setParameter("uname",userInfoEntity.getUserName());
            query.setParameter("uhead",userInfoEntity.getUserHeadUrl());
            query.setParameter("usex",userInfoEntity.getUserSex());
            query.setParameter("uemail",userInfoEntity.getUserEmail());
            query.setParameter("ubrit",userInfoEntity.getUserBrithday());
            query.setParameter("uaddre",userInfoEntity.getUserAddress());
            query.setParameter("upho",userInfoEntity.getUserPhone());
            query.setParameter("utel",userInfoEntity.getUserTel());

            count = query.executeUpdate();
            transaction.commit();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int adminUpdateUserInfo(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        try{
            String hql = "update UserInfoEntity set userLoginName = :ulogname,"
                    + "userLoginPwd = :upawd, userLevel = :uleve, userName = :uname,"
                    + "userHeadUrl = :uhead, userSex = :usex, userEmail = :uemail,"
                    + "userBrithday = :ubrit, userAddress = :uaddre, userPhone = :upho,"
                    + "userTel = :utel where id = :id";

            transaction = session.beginTransaction();
            Query query = session.createQuery(hql);
            query.setParameter("id",userInfoEntity.getId());
            query.setParameter("ulogname",userInfoEntity.getUserLoginName());
            query.setParameter("upawd",userInfoEntity.getUserLoginPwd());
            query.setParameter("uleve",userInfoEntity.getUserLevel());
            query.setParameter("uname",userInfoEntity.getUserName());
            query.setParameter("uhead",userInfoEntity.getUserHeadUrl());
            query.setParameter("usex",userInfoEntity.getUserSex());
            query.setParameter("uemail",userInfoEntity.getUserEmail());
            query.setParameter("ubrit",userInfoEntity.getUserBrithday());
            query.setParameter("uaddre",userInfoEntity.getUserAddress());
            query.setParameter("upho",userInfoEntity.getUserPhone());
            query.setParameter("utel",userInfoEntity.getUserTel());

            count = query.executeUpdate();
            transaction.commit();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int deleteUser(UserInfoEntity userInfoEntity) {
         session = getCurrentSession();
        try{
            session.delete(selectById(userInfoEntity));
            session.beginTransaction().commit();
            count = 1;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }


    //更新由于其他问题，先session.beginTransaction();在进行操作
    @Override
    public int updateUserPwd(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        try{
            String hql = "update UserInfoEntity set userLoginPwd = :lgpwd, plaintPwd = :mpwd where id = :id";
            transaction = session.beginTransaction();
            Query query = session.createQuery(hql);
            query.setParameter("id",userInfoEntity.getId());
            query.setParameter("lgpwd",userInfoEntity.getUserLoginPwd());
            query.setParameter("mpwd",userInfoEntity.getPlaintPwd());

            count = query.executeUpdate();
            transaction.commit();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    //更新由于其他问题，先session.beginTransaction();在进行操作
    @Override
    public int updateUserLev(UserInfoEntity userInfoEntity) {
        session = getCurrentSession();
        try{
            String hql = "update UserInfoEntity set userLevel = :ulv where id = :uid";
            transaction = session.beginTransaction();
            Query query = session.createQuery(hql);
            query.setParameter("ulv",userInfoEntity.getUserLevel());
            query.setParameter("uid",userInfoEntity.getId());
            count = query.executeUpdate();
            transaction.commit();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }


}
